package com.metal.gear.certification.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.metal.gear.certification.entity.dto.depart.SysDepartPermissionSearchDto;
import com.metal.gear.certification.entity.po.SysDepartPermission;
import com.metal.gear.certification.service.ISysDepartPermissionService;
import com.metal.gear.common.entity.Result;
import com.metal.gear.common.util.query.SearchHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 部门权限表
 * @Author: jeecg-boot
 * @Date: 2020-02-11
 * @Version: V1.0
 */
@Slf4j
@Tag(name = "6.部门权限表")
@RestController
@RequestMapping("/sys/sysDepartPermission")
public class SysDepartPermissionController {
    @Autowired
    private ISysDepartPermissionService sysDepartPermissionService;



    @Operation(summary = "部门权限", description = "部门权限表的分页列表查询")
    @ApiOperationSupport(order = 1)
    @GetMapping(value = "/page",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<IPage<SysDepartPermission>> queryPageList(SysDepartPermissionSearchDto searchDto,
                                                            @RequestParam(defaultValue = "1") Integer pageNo,
                                                            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        QueryWrapper<SysDepartPermission> wrapper = SearchHelper.generalQueryWrapper(searchDto);
        Page<SysDepartPermission> page = Page.of(pageNo, pageSize);
        IPage<SysDepartPermission> pageList = sysDepartPermissionService.page(page, wrapper);
        return Result.ok(pageList);
    }


    @Operation(summary = "部门权限", description = "部门权限表不进行分页查询")
    @ApiOperationSupport(order = 2)
    @GetMapping(value = "/list",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<List<SysDepartPermission>> queryPageList(SysDepartPermissionSearchDto searchDto) throws Exception {
        QueryWrapper<SysDepartPermission> wrapper = SearchHelper.generalQueryWrapper(searchDto);
        return Result.ok(sysDepartPermissionService.list(wrapper));
    }


    @Operation(summary = "添加部门权限", description = "添加部门权限")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/add",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> add(@RequestBody SysDepartPermission sysDepartPermission) {
        sysDepartPermissionService.save(sysDepartPermission);
        return Result.ok("添加成功！");
    }

    @Operation(summary = "编辑部门权限表-", description = "编辑部门权限表")
    @ApiOperationSupport(order = 3)
    @PutMapping(value = "/edit",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> edit(@RequestBody SysDepartPermission sysDepartPermission) {
        sysDepartPermissionService.updateById(sysDepartPermission);
        return Result.ok("编辑成功!");
    }

    @Operation(summary = "删除部门权限表关联关系", description = "删除部门权限表关联关系")
    @DeleteMapping(value = "/delete",produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperationSupport(order = 4)
    public Result<Boolean> delete(@RequestParam String id) {
        sysDepartPermissionService.removeById(id);
        return Result.ok("删除成功!");
    }


    @Operation(summary = "批量删除部门权限表关联关系", description = "批量删除部门权限表关联关系")
    @ApiOperationSupport(order = 4)
    @DeleteMapping(value = "/deleteBatch",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> deleteBatch(@RequestParam(name = "ids") String ids) {
        sysDepartPermissionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }


    @Operation(summary = "通过id查询部门权限关联关系", description = "通过id查询部门权限关联关系")
    @GetMapping(value = "/queryById",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<SysDepartPermission> queryById(@RequestParam String id) {
        SysDepartPermission sysDepartPermission = sysDepartPermissionService.getById(id);
        return Result.ok(sysDepartPermission);
    }

}
